﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using BLL;
using Model;
using EncryptHelper;
using DBUtility;
using UtilityHelper;
using System.Management;
using gfLibrary;

namespace IYAKASAPortal
{
    public partial class login : System.Web.UI.Page
    {
        private string _strLineNum;
        protected void Page_Load(object sender, EventArgs e)
        {
            _strLineNum = Request["LineNum"];
            btnLogin.Attributes["onclick"] = "if(document.all('txtOCode').value==''){window.alert('请输入用户名!');document.all('txtOCode').focus(); return false;}else{if(document.all('txtOPwd').value==''){window.alert('请输入密码!');document.all('txtOPwd').focus();return false;}}";
        }

        protected void btnLogin_Click(object sender, ImageClickEventArgs e)
        {
            string strUCode = txtOCode.Text.Trim().ToLower();
            string strUPwd = txtOPwd.Text.Trim().ToLower();

            if (strUCode.Trim().Length == 0)
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "请输入用户名。");
                txtOCode.Focus();
                return;
            }

            if (strUPwd.Trim().Length == 0)
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "请输入密码。");
                txtOPwd.Focus();
                return;
            }

            strUPwd = Encrypt.MD5Encrypt(strUCode + strUPwd);
            //strUPwd = Encrypt.MD5Encrypt(strUPwd);

            EMPLOYEE_USER_BLL bll = new EMPLOYEE_USER_BLL();

            EMPLOYEE_USER[] users = bll.GetModelList("EMPLOYEE_ID='" + strUCode + "'");

            if (users.Length > 1)
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "当前用户名已经重复，请联系管理员！");
                txtOCode.Focus();
                return;
            }

            if (users.Length == 1)
            {
                if (users[0].EMPLOYEE_PWD == strUPwd)
                {
                    string strSql = "if  not exists(select * from syscolumns where id=object_id('CONST_DEFINE') and name='CON_JCTYPE')";
                    strSql += "alter table CONST_DEFINE add CON_JCTYPE int";
                    bool bE = DbHelper.ExecuteSql(strSql);

                    if (!string.IsNullOrEmpty(users[0].LICENSE_NAME))
                    {
                        #region 日志记录
                        LogHelper logHelper = LogHelper.GetSingleLogHelper();

                        string strCategory = LogHelper.LogType.Normal.ToString();
                        LOG_INFO logInfo = new LOG_INFO();
                        logInfo.Category = LogHelper.LogType.Normal.ToString();
                        logInfo.HostName = System.Net.Dns.GetHostName();
                        logInfo.IPAddress = System.Net.Dns.GetHostAddresses(logInfo.HostName).GetValue(1).ToString();
                        logInfo.LogTime = DateTime.Now;
                        logInfo.Log_Infor = "系统登录成功";
                        logInfo.OpName = strUCode;
                        logInfo.Name = logHelper.GetLogName(LogHelper.LogType.Normal);

                        logHelper.WriteLog(logInfo);
                        #endregion

                        Session["UserName"] = strUCode;

                        if (users[0].LICENSE_NAME.Contains("01,"))
                        {
                            if (Request["LineNum"] != null && Request["LineNum"].ToString() != "")
                            {
                                gfLibrary.gfCookie.WriteHttpCookie("LineNum", Request["LineNum"].ToString());
                            }
                            else
                            {
                                gfLibrary.gfCookie.DeleteHttpCookie("LineNum");
                            }
                        }
                        else
                        {
                            gfLibrary.gfCookie.DeleteHttpCookie("LineNum");
                        }

                        Response.Redirect("loginmiddle.aspx");
                    }
                    else
                    {
                        gfAjaxJs.SetAlertMsg(UpdatePanel1, "当前用户" + strUCode + "没有登录权限！");
                        txtOCode.Focus();
                        return;
                    }
                }
                else
                {
                    gfAjaxJs.SetAlertMsg(UpdatePanel1, "密码输入错误。");
                    txtOPwd.Focus();
                }
            }
            else
            {
                gfAjaxJs.SetAlertMsg(UpdatePanel1, "用户名输入错误。");
                txtOCode.Focus();
            }
        }
    }
}
